package com.jingdianjichi.circle.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingdianjichi.circle.api.common.PageInfo;
import com.jingdianjichi.circle.api.common.PageResult;
import com.jingdianjichi.circle.api.enums.IsDeletedFlagEnum;
import com.jingdianjichi.circle.api.req.GetShareMomentReq;
import com.jingdianjichi.circle.api.req.RemoveShareMomentReq;
import com.jingdianjichi.circle.api.req.SaveMomentCircleReq;
import com.jingdianjichi.circle.api.vo.ShareMomentVO;
import com.jingdianjichi.circle.server.dao.ShareMomentMapper;
import com.jingdianjichi.circle.server.entity.po.ShareMoment;
import com.jingdianjichi.circle.server.service.ShareMomentService;
import com.jingdianjichi.subject.common.util.LoginUtil;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class ShareMomentServiceImpl extends ServiceImpl<ShareMomentMapper, ShareMoment> implements ShareMomentService {
    @Override
    public Boolean saveMoment(SaveMomentCircleReq req) {
        ShareMoment shareMoment = new ShareMoment();
        shareMoment.setContent(req.getContent());
        shareMoment.setCircleId(req.getCircleId());
        if(!CollectionUtils.isEmpty(req.getPicUrlList())){
            shareMoment.setPicUrls(JSON.toJSONString(req.getPicUrlList()));
        }
        shareMoment.setReplyCount(0);
        shareMoment.setCreatedBy(LoginUtil.getLoginId());
        shareMoment.setCreatedTime(new Date());
        shareMoment.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());
        boolean res = save(shareMoment);
        return res;
    }

    @Override
    public PageResult<ShareMomentVO> getMoments(GetShareMomentReq req) {
        LambdaQueryWrapper<ShareMoment> query = Wrappers.<ShareMoment>lambdaQuery().eq(ShareMoment::getCircleId, req.getCircleId())
                .eq(ShareMoment::getIsDeleted, IsDeletedFlagEnum.UN_DELETE.getCode())
                .orderByDesc(ShareMoment::getCreatedTime);
        PageInfo pageInfo = req.getPageInfo();
        Page<ShareMoment> page = new Page<>(pageInfo.getPageNo(), pageInfo.getPageSize());
        Page<ShareMoment> pageRes = super.page(page, query);
        List<ShareMoment> records = pageRes.getRecords();
        List<ShareMomentVO> collect = records.stream().map(item -> {
            ShareMomentVO shareMomentVO = new ShareMomentVO();
            shareMomentVO.setId(item.getId());
            shareMomentVO.setCircleId(item.getCircleId());
            shareMomentVO.setContent(item.getContent());
            if (Objects.nonNull(item.getPicUrls())) {
                shareMomentVO.setPicUrlList(JSON.parseArray(item.getPicUrls(), String.class));
            }
            shareMomentVO.setReplyCount(item.getReplyCount());
            shareMomentVO.setCreatedTime(item.getCreatedTime().getTime());
            return shareMomentVO;
        }).collect(Collectors.toList());
        PageResult<ShareMomentVO> pageResult = new PageResult<>();
        if (!CollectionUtils.isEmpty(collect)) {
            pageResult.setResult(collect);
            pageResult.setPageNo(pageInfo.getPageNo());
            pageResult.setPageSize(pageInfo.getPageSize());
            pageResult.setTotal((int)pageRes.getTotal());
        }
        return pageResult;
    }

    @Override
    public Boolean removeMoment(RemoveShareMomentReq req) {
        LambdaUpdateWrapper<ShareMoment> result = Wrappers.<ShareMoment>lambdaUpdate().eq(ShareMoment::getId, req.getId())
                .eq(ShareMoment::getIsDeleted, IsDeletedFlagEnum.UN_DELETE.getCode())
                .set(ShareMoment::getIsDeleted, IsDeletedFlagEnum.DELETE.getCode());
        return update(result);
    }

    @Override
    public void incrReplyCount(Long id, int count) {

    }
}
